package com.cci.kangdao.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cci.kangdao.aop.annotation.NoDuplicateSubmission;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.service.TechDealWithWorkOrderService;
import com.cci.kangdao.utilTool.ParameterTool;

/**
 * 技师处理工单相关Controller类
 * @author zhangzhuang
 *
 */

@Controller
public class TechDealWithWorkOrderController {
	
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Autowired
	private TechDealWithWorkOrderService techDealWithWorkOrderService;
	
	/**
	 * 技师点击出发按钮
	 * @param request
	 * @param response
	 * @return
	 */
	@NoDuplicateSubmission
	@RequestMapping("/techSetOut.aspx")
	public ModelAndView techSetOut(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String,Object>();;
		try {
			map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return techDealWithWorkOrderService.techSetOut(map);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}
	
	/**
	 * 服务技师接单拒单功能
	 * @param request
	 * @param response
	 * @return
	 */
	@NoDuplicateSubmission
	@RequestMapping("/serviceTechnicianReceiveOrRefuseWorkOrder.aspx")
	public ModelAndView serviceTechnicianReceiveOrRefuseWorkOrder(HttpServletRequest request, HttpServletResponse response) {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return techDealWithWorkOrderService.serviceTechnicianReceiveOrRefuseWorkOrder(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 技师点击到达按钮
	 * @param request
	 * @param response
	 * @return
	 */
	@NoDuplicateSubmission
	@RequestMapping("/clickArrive.aspx")
	public ModelAndView clickArrive(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map;
		try {
			map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return techDealWithWorkOrderService.clickArrive(map);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}
	
	/**
	 * 外出轨迹点上传接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/outGoingTrack.aspx")
	public ModelAndView outGoingTrack(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			map = ParameterTool.getParameterRequest(request,true);
			map.put("request", request);
			map.put("response", response);
			return techDealWithWorkOrderService.outGoingTrack(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}

	/**
	 * 外出轨迹点上传接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/newOutGoingTrack.aspx")
	public ModelAndView newOutGoingTrack(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			map = ParameterTool.getRequestBody(request);
			map.put("request", request);
			map.put("response", response);
			return techDealWithWorkOrderService.newOutGoingTrack(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}
	
	/**
	 * 获取外出实际里程接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getRealDistance.aspx")
	public ModelAndView getRealDitance(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return techDealWithWorkOrderService.getRealDistance(map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}

	/**
	 * 修改维修点坐标
	 * @return
	 */
	@RequestMapping(value = "/changeMaintenanceLocation.aspx",method = RequestMethod.POST)
	public ModelAndView changeMaintenanceLocation(HttpServletRequest request,HttpServletResponse response) {
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return techDealWithWorkOrderService.changeMaintenanceLocation(map);
		} catch (Exception e) {
			e.printStackTrace();
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}


	@RequestMapping(value = "/uploadRealDistance.aspx",method = RequestMethod.POST)
	public void uploadRealDistance(HttpServletRequest request,HttpServletResponse response) {
		try {
			Map<String,Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			techDealWithWorkOrderService.uploadRealDistance(map);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			ParameterTool.writeErrorResponse(response);
		}
	}

	@RequestMapping("/getRepairingOrderList.aspx")
	public void getRepairingOrderList(HttpServletRequest request,HttpServletResponse response) {
		try {
			Map<String,Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			techDealWithWorkOrderService.getRepairingOrderList(map);
		} catch (Exception e) {
			log.error("获取维修中订单列表异常",e);
			ParameterTool.writeErrorResponse(response);
		}
	}
}
